package util;


import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtils2 {
    /**
     * 发送邮件的方法
     *
     * @param to   ：给谁发邮件
     * @param code ：邮件的激活码
     * @throws UnknownHostException
     */
    public static void sendMail(String to, String code,String address,String major) throws UnknownHostException {
        String fromEmail = "572220216@qq.com";
        String eMailType = "smtp.qq.com";

        String eMailAuthPassword = "eeorlwjaijrxbcdb";
        //1.创建连接对象，连接到服务器
        Properties props = new Properties();

        props.put("mail.smtp.host", eMailType);
        //props.setProperty("host",value);
        props.put("mail.password", eMailAuthPassword);
        props.put("mail.transport.protocol", "smtp");
        props.setProperty("mail.debug", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        props.put("mail.smtp.port", "465");

//        props.put("mail.smtp.socketFactory.port", "465");
        Session session = Session.getInstance(props);
        session.setDebug(true);
        //2.创建邮件对象
        MimeMessage message = new MimeMessage(session);

        try {
            //2.1设置发件人
            message.setFrom(new InternetAddress(fromEmail));
            //2.2设置收件人
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
            //2.3设置邮件的主题
            message.setSubject("恭喜，报名成功");
			 /* message.setContent("<font color='red'><a href='http://localhost/Blog/user?method=register&acode="
	                    + code + "target='_blank'>"+"http://localhost/Blog/user?method=register&acode="+code+"</a></font>", "text/html;charset=utf-8");*/
	            /*// StringBuilder是线程不安全的
			  String localhost =  InetAddress.getLocalHost().getHostAddress();*/
//			  String localhost = "192.168.43.119:8888"; 
//			  String localhost = "192.168.1.106:8888";
//			  String localhost = "www.zzuisa.cn";
            StringBuilder sbd = new StringBuilder();
            
            sbd.append("<br/><h2>您于"+new SimpleDateFormat(" yyyy年MM月dd日 HH:mm:ss ").format(new Date())+"成功报名了英语演讲比赛：<br>地点："+address+"<br>演讲类型："+major+"</h2><br/>");
            sbd.append("<font color='red'><a href='https://jq.qq.com/?_wv=1027&k=5ioW29S' target='_blank'");
            sbd.append(">【英语报名小组1】</a></font><br/>");
            sbd.append("或者点击下面链接加入qq群:<br/>");
            sbd.append("<a href='https://jq.qq.com/?_wv=1027&k=5ioW29S'>https://jq.qq.com/?_wv=1027&k=5ioW29S</a><br/>");
            sbd.append("这是一封自动发送的邮件；如果您从未有过请求单收到这封信件，您不需要进行任何操作。");
            message.setContent(sbd.toString(), "text/html;charset=utf-8");// 发html格式的文本

            // 发送动作
            Transport transport = session.getTransport("smtp");
            transport.connect(eMailType, fromEmail, eMailAuthPassword);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        //3.发送一封激活邮件
    }
}
